﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.SalePerformance.Record;
using Common.Contract.SalePerformance.Request;
using Common.Contract.SalePerformance.Response;
using Common.Service.Command;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace SalePerformance.Core.ServiceCommand
{
    public class RetrieveSaleKpiByDaoServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveSaleKpiByDaoRequest, RetrieveSaleKpiByDaoResponse>
    {
        #region Properties
        public string Dao;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveSaleKpiByDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveSaleKpiByDaoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Dao))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.Dao;
            }
        }

        protected override bool OnExecute(RetrieveSaleKpiByDaoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<SaleKPIRecord> records = new List<SaleKPIRecord>();
                List<TblSalePerformanceKpi> listLoans = new List<TblSalePerformanceKpi>();
                listLoans = Providers.SalePerformance.RetrieveSaleKpiByDao(Dao);
                if (listLoans != null)
                {
                    records = listLoans.Select(x => new SaleKPIRecord
                               {
                                   GroupId = x.GroupId,
                                   KpiGroup = x.KpiGroup,
                                   KpiName = x.KpiName,
                                   KpiId = x.KpiId,
                                   KpiUnit = x.KpiUnit,
                                   KpiWeight = x.KpiWeight,
                                   LevelStaff = x.LevelStaff,
                                   Target = x.Target,
                                   Act = x.Act,
                                   Real = x.Real,
                                   Url = x.Url
                               }).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = 0,
                    PageSize = records.Count,
                    TotalRecords = records.Count
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
